package org.example.utils.order;

import org.example.dao.dto.order.OrderDto;
import org.example.skytool.json.JackSonUtil;
import org.example.skytool.redis.RedisUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import static org.example.skytool.redis.RedisConstants.*;

/**
 * @author 22449
 */
@Component
public class RedisOrderService {

    @Resource
    private RedisUtils redisUtils;

    /**
     * 获取记录次数的header
     */
    public String getCountHeader(String tradeNo){
        return ORDER_MOUDLE +  ORDER_COUNT_CONTROLLER + tradeNo ;
    }

    public Long addCount(String tradeNo){
        return redisUtils.incrBy(getCountHeader(tradeNo),1L,30L,TimeUnit.MINUTES);
    }

    public void delCount(String tradeNo){
         redisUtils.del(getCountHeader(tradeNo));
    }


    /**
     * 获取待支付header
     */
    public String getNoPayHeader() {
        return ORDER_MOUDLE + ORDER_MQ_CONTROLLER + ORDER_MQ_NOPAY_KIND;
    }


    public void setNoPayOrder(String tradeNo) {
        String key = getNoPayHeader();
        redisUtils.setAdd(key, tradeNo);
    }

    public Long getNoPaySize() {
        String key = getNoPayHeader();
        return redisUtils.setSize(key);
    }
    public Set<String> getNoPayList(){
        String key = getNoPayHeader();
        return redisUtils.setGetAll(key);
    }

    public Boolean getNoPayOrder(String tradeNo) {
        String key = getNoPayHeader();
        return redisUtils.setIsMember(key, tradeNo);
    }

    public void delNoPayOrder(String tradeNo) {
        String key = getNoPayHeader();
        redisUtils.setDel(key, tradeNo);
    }


    /**
     * 获取待确认header
     */
    public String getEnsureHeader(String field) {
        return ORDER_MOUDLE + ORDER_MQ_CONTROLLER + ORDER_MQ_ENSURE_KIND + field;
    }

    public void setEnsureOrder(String field, OrderDto orderDto) {
        String key = getEnsureHeader(field);
        redisUtils.stringSet(key, JackSonUtil.encode(orderDto),15L, TimeUnit.MINUTES);
    }


    public OrderDto getEnsureOrder(String field) {
        String key = getEnsureHeader(field);
        String json = redisUtils.stringGet(key);
        return JackSonUtil.parse(OrderDto.class, json);
    }

    public void delEnsureOrder(String field) {
        String key = getEnsureHeader(field);
        redisUtils.del(key);
    }


    /**
     * 前端确认支付
     */
    public String getPayHeader() {
        return ORDER_MOUDLE + ORDER_MQ_CONTROLLER + ORDER_MQ_PAY_KIND;
    }

    public Long getPaySize() {
        String key = getPayHeader();
        return redisUtils.setSize(key);
    }

    public void setPayOrder(String tradeNo) {
        String key = getPayHeader();
        redisUtils.setAdd(key, tradeNo);
    }

    public Boolean getPayOrder(String tradeNo) {
        String key = getPayHeader();
        return redisUtils.setIsMember(key, tradeNo);
    }

    public void delPayOrder(String tradeNo) {
        String key = getPayHeader();
        redisUtils.setDel(key, tradeNo);
    }
    public Set<String> getPayList(){
        String key = getPayHeader();
        return redisUtils.setGetAll(key);
    }
    /**
     * 用户正在支付
     */
    public String getPayingHeader() {
        return ORDER_MOUDLE + ORDER_MQ_CONTROLLER + ORDER_MQ_PAYING_KIND;
    }

    public Long getPayingSize() {
        String key = getPayingHeader();
        return redisUtils.setSize(key);
    }

    public void setPayingOrder(String tradeNo) {
        String key = getPayingHeader();
        redisUtils.setAdd(key, tradeNo);
    }

    public Boolean getPayingOrder(String tradeNo) {
        String key = getPayingHeader();
        return redisUtils.setIsMember(key, tradeNo);
    }
    public Set<String> getPayingList(){
        String key = getPayingHeader();
        return redisUtils.setGetAll(key);
    }
    public void delPayingOrder(String tradeNo) {
        String key = getPayingHeader();
        redisUtils.setDel(key, tradeNo);
    }

}
